<?php

class Master_kategori extends CI_Controller {
    
    function __construct() {
        parent::__construct();
		$this->load->library(array('flexigrid','form_validation'));		
		$this->load->helper(array('form','url','codegen_helper','flexigrid'));
		$this->load->model('codegen_model','',TRUE);
	}	
	
	function index(){
		$this->manage();
	}

	function manage(){
		$colModel['NO'] = array('No', 20, TRUE, 'center', 0);
        $colModel['NAMA_KATEGORI'] = array('Nama Kategori', 150, FALSE, 'center', 1);
		$colModel['EDIT'] = array('Edit', 70, FALSE, 'center', 0);
        $gridParams = array(
            'width' => 'auto',
            'height' => '390',
            'rp' => 15,
            'rpOptions' => '[10,15,20,25,40]',
            'pagestat' => 'Displaying: {from} to {to} of {total} items.',
            'blockOpacity' => 0.5,
            'title' => 'Master Kategori'
        );
        $buttons[] = array('Tambah', 'add', 'master_user');
        $buttons[] = array('Hapus', 'delete', 'master_user');
        $buttons[] = array('separator');
        $buttons[] = array('Pilih Semua', 'add', 'master_user');
        $buttons[] = array('Hapus Pilihan', 'delete', 'master_user');
        $buttons[] = array('separator');
		
        $grid_js = build_grid_js('flex1', site_url("master_kategori/gridKategori"), $colModel, 'ID_KATEGORI', 'ASC', $gridParams, $buttons);

        $data['title']	= "Master Kategori";
        $data['info']	= "Master Kategori";
        $data['js_grid']= $grid_js;


        $data['added_js'] =
                "<script type='text/javascript'>
			function master_user(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Hapus Pilihan')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Tambah')
				{
					location.href='" . base_url() . "index.php/master_kategori/add/'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin mengaktifkan ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" .base_url('index.php/Pasien/deletePasien')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}
			} 
			</script>
			";
		$data["content"]	= $this->load->view("grid",$data,true);
		$data["active"]		= "master";
		$this->load->view("home_final",$data);
    }
	function gridKategori(){
		$valid_fields = array('ID_KATEGORI', 'NAMA_KATEGORI');
        $this->flexigrid->validate_post('ID_ANGGOTA', 'ASC', $valid_fields);
        $records = $this->codegen_model->getFG("master_kategori");
        $this->output->set_header($this->config->item('json_header'));
        $no = 1;
        foreach ($records['records']->result() as $row) {
            $record_items[] = array($row->ID_KATEGORI,
                $no,
				$row->NAMA_KATEGORI,
				"<a href='".site_url("/master_kategori/edit/".$row->ID_KATEGORI)."'><img src='". base_url()."assets/fg/images/grid/edit.png'></a>"
            );
            $no++;
        }
        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
    function add(){        
        $this->load->library('form_validation');    
		$this->data['custom_error'] = '';
		
        if ($this->form_validation->run('master_kategori') == false)
        {
             $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);

        } else
        {                            
            $data = array(
                    'NAMA_KATEGORI' => set_value('NAMA_KATEGORI')
            );
           
			if ($this->codegen_model->add('master_kategori',$data) == TRUE)
			{
				//$this->data['custom_error'] = '<div class="form_ok"><p>Added</p></div>';
				// or redirect
				redirect(base_url().'index.php/master_kategori/manage/');
			}
			else
			{
				$this->data['custom_error'] = '<div class="form_error"><p>An Error Occured.</p></div>';

			}
		}		   
		$data["content"]= $this->load->view('master_kategori_add', $this->data,true);
		$data["active"]	= "master";
		$this->load->view("home_final", $data);
        //$this->template->load('content', 'master_kategori_add', $this->data);
    }	
    
    function edit(){        
        $this->load->library('form_validation');    
		$this->data['custom_error'] = '';
		
        if ($this->form_validation->run('master_kategori') == false)
        {
             $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);

        } else
        {                            
            $data = array(
                    'NAMA_KATEGORI' => $this->input->post('NAMA_KATEGORI')
            );
           
			if ($this->codegen_model->edit('master_kategori',$data,'ID_KATEGORI',$this->input->post('ID_KATEGORI')) == TRUE)
			{
				redirect(base_url().'index.php/master_kategori/manage/');
			}
			else
			{
				$this->data['custom_error'] = '<div class="form_error"><p>An Error Occured</p></div>';

			}
		}

		$this->data['result'] = $this->codegen_model->get('master_kategori','ID_KATEGORI,NAMA_KATEGORI','ID_KATEGORI = '.$this->uri->segment(3),NULL,NULL,true);
		
		$data["content"]= $this->load->view('master_kategori_edit', $this->data,true);
		$data["active"]	= "master";
		$this->load->view("home_final", $data);		
        //$this->template->load('content', 'master_kategori_edit', $this->data);
    }
	
    function delete(){
            $ID =  $this->uri->segment(3);
            $this->codegen_model->delete('master_kategori','ID_KATEGORI',$ID);             
            redirect(base_url().'index.php/master_kategori/manage/');
    }
}

/* End of file master_kategori.php */
/* Location: ./system/application/controllers/master_kategori.php */